<?php

namespace App\Admin\Controllers;

use App\Models\AdminOperationLog;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AdminOperationLogController extends AdminController
{
    protected $title = '操作日志';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $admin_user = Admin::user();
        $description = request()->get('description', '');

        $builder = AdminOperationLog::query()->with(['user']);
        if ($description) {
            $encode_keyword = str_replace('"', '', json_encode($description));
            $encode_keyword = str_replace("\\", '_', $encode_keyword);
            $builder->where('description', 'like', '%'.$encode_keyword.'%');
        }
        return Grid::make($builder, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            // $grid->column('id')->sortable();
            // $grid->column('user_id');
            $grid->column('user_name', '操作人')->display(function($v) {
                if ($v) {
                    return $v;
                }
                return $this->user ? $this->user->name : '';
            });
            $grid->column('ip', 'IP');
            $grid->column('description', '简述')->display(function ($v) {
                $html = '';
                foreach($v as $text) {
                    $html .= '<p>'.$text.'</p>';
                }
                return $html;
            });
            $grid->column('created_at', '时间')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand()->panel();
                $filter->equal('ip')->width(3);
                $filter->equal('user_name', '操作人')->width(3);
                $filter->like('description', '简述')->ignore()->width(3);
                $filter->between('created_at', '时间')->datetime()->width(3);
        
            });
            $grid->disableCreateButton()->disableRefreshButton()->disableFilterButton()->disableActions()->disableRowSelector();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AdminOperationLog(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('user_name');
            $show->field('path');
            $show->field('method');
            $show->field('ip');
            $show->field('input');
            $show->field('description');
            $show->field('relation_table');
            $show->field('relation_id');
            $show->field('detail');
            $show->field('type');
            $show->field('gid');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AdminOperationLog(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('user_name');
            $form->text('path');
            $form->text('method');
            $form->text('ip');
            $form->text('input');
            $form->text('description');
            $form->text('relation_table');
            $form->text('relation_id');
            $form->text('detail');
            $form->text('type');
            $form->text('gid');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
